<?php

namespace Home\Controller\Saleman;

use Think\Controller;

class PublicController extends Controller {

    public function UpCoordinate() {
    	$REQUEST = ( array ) I ( 'request.' );
        $userId = $REQUEST['uid'];
        $longitude = $REQUEST['longitude'];
        $latitude = $REQUEST['latitude'];
        if (empty($longitude)) {
            responseJson(0, '未获取到经度');
            exit;
        }
        if (empty($latitude)) {
            responseJson(0, '未获取到纬度');
            exit;
        }
        if (empty($userId)) {
            responseJson(0, '未获取到用户ID');
            exit;
        }

        $data = array(
            'uid' => $userId,
            'longitude' => $longitude,
            'latitude' => $latitude
        );
        S($userId, $data);

        responseJson(1, 'success',$data);
        exit;
    }
    
    //获取目标诊所经纬度及地址接口
    public function getILatLngAddress() {
    		$REQUEST = ( array ) I ( 'request.' );
	    	$userId = $REQUEST['uid'];
	    	$longitude = $REQUEST['longitude'];
	    	$latitude = $REQUEST['latitude'];
	    	$address = $REQUEST['address'];
    	if (empty($longitude)) {
    		responseJson(0, '未获取到经度');
    		exit;
    	}
    	if (empty($latitude)) {
    		responseJson(0, '未获取到纬度');
    		exit;
    	}
    	if (empty($userId)) {
    		responseJson(0, '未获取到用户ID');
    		exit;
    	}
    	if (empty($address)) {
    		responseJson(0, '未获取到地址');
    		exit;
    	}
    
	    	$data = array(
	    			'uid' => $userId,
	    			'longitude' => $longitude,
	    			'latitude' => $latitude,
	    			'address' => $address
	    	);
	    	S($userId,$data);
	   // 	session($userId,$data);
	    	responseJson(1, 'success',$data);
	    	exit;
    }
    
    
    
    //获取区域诊所位置[客户端调用接口]
    public function getILatLngForMap() {
    	$REQUEST = ( array ) I ( 'request.' );
    	$userId = $REQUEST['uid'];
    	$longitude = $REQUEST['longitude'];
    	$latitude = $REQUEST['latitude'];
    	if (empty($longitude)) {
    		responseJson(0, '未获取到经度');
    		exit;
    	}
    	if (empty($latitude)) {
    		responseJson(0, '未获取到纬度');
    		exit;
    	}
    	if (empty($userId)) {
    		responseJson(0, '未获取到用户ID');
    		exit;
    	}
    
    	$data = array(
    			'uid' => $userId,
    			'longitude' => $longitude,
    			'latitude' => $latitude,
    	);
    	S($userId,$data);
    	// 	session($userId,$data);
    	responseJson(1, 'success',$data);
    	exit;
    }
    
    //获取诊所位置
    public function getLatLngAddress() {
    
    		$REQUEST = ( array ) I ( 'request.' );
    		$userId = $REQUEST['uid'];
    		$data=S($userId);
    		if(!empty(S($userId))){
    			S($userId,null);
    			responseJson(1, 'success',$data);
    			
    			exit;	
    		}else{
    			responseJson(2, 'wait');
    			exit;
    		}
    }
    
    
    //获取区域位置
    public function getLatLngForSaleMap() {
    
    	$REQUEST = ( array ) I ( 'request.' );
    	$userId = $REQUEST['uid'];
    	$data=S($userId);
    	if(!empty(S($userId))){
    		S($userId,null);
    		responseJson(1, 'success',$data);
    		 
    		exit;
    	}else{
    		responseJson(2, 'wait');
    		exit;
    	}
    }
    
    //获取签到位置
    public function getSign() {
    $REQUEST = ( array ) I ( 'request.' );
    		$userId = $REQUEST['uid'];
    		$data=S($userId);
    		if(!empty(S($userId))){
    			S($userId,null);
    			responseJson(1, 'success',$data);
    			
    			exit;	
    		}else{
    			responseJson(2, 'wait');
    			exit;
    		}
    }


    /**
     * 保存极光RegID
     * @param $UID
     * @param string $RegId
     */
    public function getRegID($UID,$RegId){

        if(!$UID&& !$RegId) responseJson('1','参数错误');
        $data=M('SalemanPush')->where(array('saleman_id'=>$UID))->find();
        if($data){
            //修改
            $d = array(
                'register_id'=>$RegId,
            );
            $res = M('SalemanPush')->where(array('saleman_id'=>$UID))->save($d);

            if($res!==false){
                responseJson('0','操作成功');
            }else{
                responseJson('2','操作失败');
            }

        }else{
            //新增
            $d = array(
                'saleman_id'=>$UID,
                'register_id'=>$RegId,
            );
            $res = M('SalemanPush')->add($d);
            if($res===false){
                responseJson('2','操作失败');
            }else{
                responseJson('0','操作成功');
            }
        }
    }


}

